package myjava.method;

/**
 * @author Yancy
 * @program springioctest
 * @create 2020-11-16
 */
public class Date {

  private int day, month, year;

  Date() {
    day = 31;
    month = 1;
    year = 1998;
  }

  public Date(int day, int month, int year) {
    this.day = day;
    this.month = month;
    this.year = year;
  }

  Date(Date d) {
    day = d.day;
    month = d.month;
    year = d.year;
  }

  public String printDate() {
    return (day + "/" + month + "/" + year);
  }

  static final int MAX_M = 12;
  static final int MIN_M = 1;

  boolean existed = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

  public Date tomorrow() { // 计算明天
    Date d = new Date();
    d.day++;
    if (d.day > d.daysInMonth()) {
      d.day = 1;
      d.month++;
      if (d.month > MAX_M) {
        d.month = 1;
        d.year++;
      }
    }
    return d;
  }

  public int daysInMonth() {

    if (month < MIN_M || month > MAX_M) {
      return -1;
    }
    switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        return 31;
      case 4:
      case 6:
      case 9:
      case 11:
        return 30;
      default:
        // 判断闰年
        if (existed) {
          return 29;
        } else {
          return 28;
        }
    }
  }

  public static void main(String[] args) {
    Date d1 = new Date();
    Date d2 = new Date(20, 3, 2000);
    Date d3 = new Date(d1);
    System.out.println("d1:" + d1.printDate());
    System.out.println("d2:"+d2.printDate());
    System.out.println("d3:" + d3.printDate());
    System.out.println("明天是：" + d3.tomorrow().printDate());
  }
}
